﻿using CYZH.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CYZH.Service.Drought
{
    public class CommonService
    {
        public static string RegionaStatistics(string tempRaster,string aoiShpFile)
        {
            string strjson,msg;
            int res = CYZHLibrary.Common.GdalTools.RegionaStatistics(tempRaster, out strjson, aoiShpFile, "code", out msg);

            Logger.Singleton.Debug(msg);
            return strjson;
        }
        public static int saveSPIRasterResult(string filename, string fileTime, string xzqhcode, string layerName, string alg, string mouth, string days, string xun, string timeType, string str_static,string scales)
        {

            filename = Path.GetFileNameWithoutExtension(filename);
            string sql = String.Format("insert into rasterresults(layername,scope,filetime,filename,algorithm,year,mouth,days,timetype,createby,createtime,updatetime,updateby,xun,statictext,spiscales) values('{0}','{1}','{2}','{3}','{9}','{4}','{5}','{6}','{7}','wyf',STR_TO_DATE('{8}','%Y-%m-%d %H:%i:%s'),STR_TO_DATE('{8}', '%Y-%m-%d %H:%i:%s'),'wyf','{10}','{11}','{12}')", layerName, xzqhcode, fileTime, filename, fileTime.Substring(0, 4), mouth, days, timeType, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), alg, xun, str_static, scales);
            return MysqlHelper.Save2DB(sql);
        }
        public static int saveRasterResult(string filename, string fileTime, string xzqhcode, string layerName, string alg, string mouth, string days, string xun, string timeType, string str_static,string mergealg)
        {

            filename = Path.GetFileNameWithoutExtension(filename);
            string sql = "";
            if (string.IsNullOrEmpty(mergealg))
            {
                sql = String.Format("insert into rasterresults(layername,scope,filetime,filename,algorithm,year,mouth,days,timetype,createby,createtime,updatetime,updateby,xun,statictext) values('{0}','{1}','{2}','{3}','{9}','{4}','{5}','{6}','{7}','wyf',STR_TO_DATE('{8}','%Y-%m-%d %H:%i:%s'),STR_TO_DATE('{8}', '%Y-%m-%d %H:%i:%s'),'wyf','{10}','{11}')", layerName, xzqhcode, fileTime, filename, fileTime.Substring(0, 4), mouth, days, timeType, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), alg, xun, str_static);

            }
            else
            {
                sql = String.Format("insert into rasterresults(layername,scope,filetime,filename,algorithm,year,mouth,days,timetype,createby,createtime,updatetime,updateby,xun,statictext,mergealg) values('{0}','{1}','{2}','{3}','{9}','{4}','{5}','{6}','{7}','wyf',STR_TO_DATE('{8}','%Y-%m-%d %H:%i:%s'),STR_TO_DATE('{8}', '%Y-%m-%d %H:%i:%s'),'wyf','{10}','{11}','{12}')", layerName, xzqhcode, fileTime, filename, fileTime.Substring(0, 4), mouth, days, timeType, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), alg, xun, str_static,mergealg);

            }
            return MysqlHelper.Save2DB(sql);
        }
        public static List<ModisResultBean> querySPIRasterResult(string xzqhcode, string fileTime,string scale)
        {
            List<ModisResultBean> list = new List<ModisResultBean>();
            string sql = String.Format("select distinct id ,layername,filename from rasterresults where scope='{0}' and filetime='{1}' and ALGORITHM='SPI' and spiscales='{2}'", xzqhcode, fileTime, scale);
            //DataTable dt=OracleHelper.ExecuteDataTable(sql);
            DataTable dt = MysqlHelper.ExecuteDataTable(sql);
            foreach (DataRow myRow in dt.Rows)
            {
                ModisResultBean mrb = new ModisResultBean();
                mrb.ID = myRow[0].ToString();

                mrb.LayerName = myRow[1].ToString();
                mrb.FileName = myRow[2].ToString();
                list.Add(mrb);
            }
            return list;
        }
        public static List<ModisResultBean> queryRasterResult(string xzqhcode, string fileTime, string alg, string timetype,string mergealg)
        {
            string sql = "";
            List <ModisResultBean> list = new List<ModisResultBean>();
            if (string.IsNullOrEmpty(mergealg))
            {
                sql= String.Format("select distinct id ,layername,filename from rasterresults where scope='{0}' and filetime='{1}' and ALGORITHM='{2}' and timetype='{3}'", xzqhcode, fileTime, alg, timetype);
            }
            else
            {
                sql = String.Format("select distinct id ,layername,filename from rasterresults where scope='{0}' and filetime='{1}' and ALGORITHM='{2}' and timetype='{3}' and mergealg='{4}'", xzqhcode, fileTime, alg, timetype,mergealg);
            }
            
            //DataTable dt=OracleHelper.ExecuteDataTable(sql);
            DataTable dt = MysqlHelper.ExecuteDataTable(sql);
            foreach (DataRow myRow in dt.Rows)
            {
                ModisResultBean mrb = new ModisResultBean();
                mrb.ID = myRow[0].ToString();

                mrb.LayerName = myRow[1].ToString();
                mrb.FileName = myRow[2].ToString();
                list.Add(mrb);
            }

            return list;

        }

    }
}
